package com.stox.ui.javafx.charting.widget;

import java.util.Locale;
import java.util.Map;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;

import org.springframework.context.ApplicationContext;

import com.stox.ui.javafx.charting.draw.mode.DrawingMode;
import com.stox.ui.javafx.charting.model.enumeration.ChartType;
import com.stox.ui.javafx.charting.view.ChartingView;


public class ChartingToolBar extends FlowPane {

	private final ChartingView chartingView;
	
	private ApplicationContext applicationContext;
	
	public ChartingToolBar(ChartingView chartingView, ApplicationContext applicationContext) {
		this.chartingView = chartingView;
		this.applicationContext = applicationContext;
		
		getStyleClass().add("charting-tool-bar");
		
		chartTypeToolBar();
		drawingToolBar();
	}	

	private void drawingToolBar(){
		HBox hbox = new HBox();
		getChildren().add(hbox);
		ToggleGroup toggleGroup = new ToggleGroup();
		Map<String, DrawingMode> drawingModes = applicationContext.getBeansOfType(DrawingMode.class);
		for(String beanName : drawingModes.keySet()){
			DrawingMode drawingMode = drawingModes.get(beanName);
			ToggleButton toggleButton = new ToggleButton();
			toggleButton.getStyleClass().addAll("charting-button");
			Region graphics = new Region();
			graphics.getStyleClass().add(beanName);
			toggleButton.setGraphic(graphics);
			toggleButton.setToggleGroup(toggleGroup);
			toggleButton.setTooltip(new Tooltip(applicationContext.getMessage(beanName, null, Locale.getDefault())));
			hbox.getChildren().add(toggleButton);
			toggleButton.selectedProperty().addListener(new ChangeListener<Boolean>(){
				@Override
				public void changed(ObservableValue<? extends Boolean> observable,
						Boolean oldValue, Boolean newValue) {
					if(newValue){
						chartingView.setDrawingMode(drawingModes.get(beanName));
						chartingView.update();
					}else if(oldValue){
						DrawingMode currentDrawingMode = chartingView.getDrawingMode();
						if(drawingMode.equals(currentDrawingMode)){
							chartingView.setDrawingMode(null);
						}
					}
				}
			});
		}
		
		Button clearButton = new Button();
		clearButton.getStyleClass().addAll("charting-button");
		Region graphics = new Region();
		graphics.getStyleClass().add("eraser");
		clearButton.setGraphic(graphics);
		clearButton.setTooltip(new Tooltip(applicationContext.getMessage("Clear", null, Locale.getDefault())));
		hbox.getChildren().add(clearButton);
		clearButton.setOnAction(new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				chartingView.getPrimaryChart().getShapes().clear();
				for(Chart chart : chartingView.getSecondaryCharts()){
					chart.getShapes().clear();
				}
				chartingView.update();
			}
		});
	}
	
	private void chartTypeToolBar(){
		HBox hbox = new HBox();
		getChildren().addAll(hbox);
		ToggleGroup chartTypeToggleGroup = new ToggleGroup();
		for(ChartType chartType : ChartType.values()){
			chartTypeButton(chartTypeToggleGroup, hbox, chartType);
		}
	}
	
	private void chartTypeButton(ToggleGroup toggleGroup, HBox hbox, ChartType chartType){
		ToggleButton toggleButton = new ToggleButton();
		toggleButton.getStyleClass().addAll("charting-button");
		Region graphics = new Region();
		graphics.getStyleClass().add(chartType.name());
		toggleButton.setGraphic(graphics);
		toggleButton.setToggleGroup(toggleGroup);
		toggleButton.setTooltip(new Tooltip(applicationContext.getMessage(chartType.getCode(), null, Locale.getDefault())));
		hbox.getChildren().add(toggleButton);
		toggleButton.selectedProperty().addListener(new ChangeListener<Boolean>(){
			@Override
			public void changed(ObservableValue<? extends Boolean> observable,
					Boolean oldValue, Boolean newValue) {
				if(newValue){
					chartingView.getPrimaryChart().getFrontPlot().setChartType(chartType);
					chartingView.update();
				}
			}
		});
	}
	
	
}
